package com.byjh.web.controller.common;

import com.byjh.common.annotation.Anonymous;
import com.byjh.common.config.ByjhConfig;
import com.byjh.common.constant.Constants;
import com.byjh.common.core.domain.AjaxResult;
import com.byjh.common.core.domain.FileObject;
import com.byjh.common.core.domain.R;
import com.byjh.common.utils.StringUtils;
import com.byjh.common.utils.aliyun.OssUtil;
import com.byjh.common.utils.file.FileUploadUtils;
import com.byjh.common.utils.file.FileUtils;
import com.byjh.framework.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用请求处理
 *
 * @author dhq
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);
    private final OssUtil ossUtil;

    public UploadController(OssUtil ossUtil) {
        this.ossUtil = ossUtil;
    }


    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/oss")
    @Anonymous
    public R<FileObject> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            return R.ok(ossUtil.uploadFile(file));
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

}
